/*
 * Copyright (C) 2010 Juan José Ortilles jortilles@gmail.com
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 3 of the License, or 
 * any later version.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 *
 */
package net.juantxu.pentaho.launcher.ejecutores;

import java.io.File;
import java.util.Properties;

import net.juantxu.pentaho.launcher.utilidades.CargaProperties;
import net.juantxu.pentaho.launcher.utilidades.ValidaEjecutableNix;
import net.juantxu.pentaho.launcher.utilidades.ValidaEjecutableWin;
import net.juantxu.pentaho.launcher.utilidades.runGuin;

import org.apache.log4j.Logger;

public class EjecutaDataIntegration implements EjecutaPentaho {
	static Logger log = Logger.getLogger(EjecutaDataIntegration.class);

	public int lanza() {
		String os = "";
		int res = 0;
		log.debug("lanzando data integration");
		os = System.getProperty("os.name");
		log.debug("sistema operativo :".concat(os));

		if (os.toLowerCase().contains("win")) {
			res = ejecutaWin();
		} else {
			log.debug("es un sistema *NIX");
			res = ejecutaNix();
		}
		return res;
	}

	public int ejecutaWin() {
		int res = 0;
		Properties prop = new CargaProperties().Carga();
		final File miPath = new File(prop.getProperty("dataIntegrationRelativePath"));
		log.debug("Ejecutando en :" + miPath.getAbsolutePath());
		if (new ValidaEjecutableWin().valida(prop.getProperty("dataIntegrationRelativePath")
				+ "\\spoon.bat")) {
			 String comando= "\"" + miPath.getAbsolutePath() +  "\\spoon.bat\"";
			 new runGuin().ejecuta(comando);
		       try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		} else {
			// no se encuetra el ejecutable
			log.error("no se encuentra el ejecutable");
			log.error("valida devolvió false");
			res = 7;
		}
		return res;
	}

	public int ejecutaNix() {
		Properties prop = new CargaProperties().Carga();
		int res = 0;
		if (new ValidaEjecutableNix().valida(prop
				.getProperty("dataIntegrationRelativePath")
				+ "/spoon.sh")) {
			File miPath = new File(prop
					.getProperty("dataIntegrationRelativePath"));
			log.debug("Ejecutando en :" + miPath.getAbsolutePath());
			Runtime r = Runtime.getRuntime();
			try {
				r.exec("./spoon.sh", null, miPath);
				Thread.sleep(3000);
			} catch (Exception e) {
				e.printStackTrace();
				res = 1;
			}

		} else {
			// no se encuetra el ejecutable
			log.error("no se encuentra el ejecutable");
			res = 7;
		}

		return res;
	}
}
